<?php

namespace Fize\Third\Dify\Datasets;

use CURLFile;
use Fize\Third\Dify\ApiAbstract;

/**
 * 文档
 */
class Document extends ApiAbstract
{

    /**
     * 通过文件创建文档
     * @param string            $dataset_id 知识库ID
     * @param array|string      $data       data字段
     * @param string            $file       需要上传的文件
     * @param string|null       $file_name
     * @param array|string|null $retrieval_model
     * @param string|null       $embedding_model
     * @param string|null       $embedding_model_provider
     * @return array
     */
    public function createByFile(
        string $dataset_id, $data, string $file, string $file_name = null,
               $retrieval_model = null, string $embedding_model = null, string $embedding_model_provider = null
    ): array
    {
        $path = "/datasets/{$dataset_id}/document/create-by-file";
        if (is_null($file_name)) {
            $file_name = basename($file);
        }
        $params = [
            'file' => new CURLFile($file, null, $file_name)
        ];
        if (is_array($data)) {
            $data = json_encode($data);
        }
        $params['data'] = $data;
        if (!is_null($retrieval_model)) {
            if (is_array($retrieval_model)) {
                $retrieval_model = json_encode($retrieval_model);
            }
            $params['retrieval_model'] = $retrieval_model;
        }
        if (!is_null($embedding_model)) {
            $params['embedding_model'] = $embedding_model;
        }
        if (!is_null($embedding_model_provider)) {
            $params['embedding_model_provider'] = $embedding_model_provider;
        }
        return $this->httpPost($path, $params, false);
    }

    /**
     * 通过文件更新文档
     * @param $dataset_id
     * @param $document_id
     * @param $data
     * @param $file
     * @param $file_name
     * @param $retrieval_model
     * @param $embedding_model
     * @param $embedding_model_provider
     * @return array
     */
    public function updateByFile(
        $dataset_id, $document_id, $data, $file, $file_name = null,
        $retrieval_model = null, $embedding_model = null, $embedding_model_provider = null
    ): array
    {
        $path = "/datasets/{$dataset_id}/documents/{$document_id}/update-by-file";
        if (is_null($file_name)) {
            $file_name = basename($file);
        }
        $params = [
            'file' => new CURLFile($file, null, $file_name)
        ];
        if (is_array($data)) {
            $data = json_encode($data);
        }
        $params['data'] = $data;
        if (!is_null($retrieval_model)) {
            if (is_array($retrieval_model)) {
                $retrieval_model = json_encode($retrieval_model);
            }
            $params['retrieval_model'] = $retrieval_model;
        }
        if (!is_null($embedding_model)) {
            $params['embedding_model'] = $embedding_model;
        }
        if (!is_null($embedding_model_provider)) {
            $params['embedding_model_provider'] = $embedding_model_provider;
        }
        return $this->httpPost($path, $params, false);
    }

    /**
     * 删除文档
     * @param string $dataset_id  知识库ID
     * @param string $document_id 文档ID
     * @return bool 成功返回true，失败返回false
     */
    public function delete(string $dataset_id, string $document_id): bool
    {
        $uri = "/datasets/{$dataset_id}/documents/{$document_id}";
        $content = $this->httpDelete($uri);
        if ($content === true) {
            return true;
        }
        $content = trim(str_replace(["\r", "\n"], '', $content));
        if ($content != 204) {
            return false;
        }
        return true;
    }

    /**
     * 知识库文档列表
     * @param string      $dataset_id 知识库ID
     * @param int|null    $page       页码
     * @param int|null    $limit      返回条数
     * @param string|null $keyword    搜索关键词
     * @return array
     */
    public function getList(string $dataset_id, int $page = null, int $limit = null, string $keyword = null): array
    {
        $params = [];
        if (!is_null($page)) {
            $params[] = "page={$page}";
        }
        if (!is_null($limit)) {
            $params[] = "limit={$limit}";
        }
        if (!is_null($keyword)) {
            $params[] = "keyword={$keyword}";
        }
        $uri = "/datasets/{$dataset_id}/documents";
        if (!empty($params)) {
            $uri .= '?' . implode('&', $params);
        }
        return $this->httpGet($uri);
    }
}